package pl.edu.agh.mobsys.utils;

import java.util.List;

import pl.edu.agh.mobsys.R;
import pl.edu.agh.mobsys.data.Contact;
import pl.edu.agh.mobsys.data.SmoaContact;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * Adapter dla listy kontaków.
 * */
public class ContactAdapter extends BaseAdapter {

	@SuppressWarnings("unused")
	private static final String CLASSTAG = ContactAdapter.class.getSimpleName();
	private final List<Contact> contacts;

	private final Context ctx;

	public ContactAdapter(Context context, List<Contact> reviews) {
		ctx = context;
		this.contacts = reviews;
	}

	public int getCount() {
		return contacts.size();
	}

	public Object getItem(int position) {
		return contacts.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		Contact c = this.contacts.get(position);
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) ctx
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.contacts_row, null);
		}
		try {
			((TextView) v.findViewById(R.id.text)).setText(c.getAlias());
			((ImageView) v.findViewById(R.id.image))
					.setImageResource(R.drawable.green_dot);
			((TextView) v.findViewById(R.id.contact_smoa))
					.setVisibility(c instanceof SmoaContact ? View.VISIBLE
							: View.GONE);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return v;
	}
	
}
